<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="挂号专家" prop="makdDoctorId">
        <el-input
          v-model="queryParams.makdDoctorId"
          placeholder="请输入挂号专家"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="挂号时间" prop="makeDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.makeDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择挂号时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所患疾病" prop="makeStatus">
        <el-input
          v-model="queryParams.makeStatus"
          placeholder="请输入所患疾病"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="你的姓名" prop="makeName">
        <el-input
          v-model="queryParams.makeName"
          placeholder="请输入你的姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="makePhone">
        <el-input
          v-model="queryParams.makePhone"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:make:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:make:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:make:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:make:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="makeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="挂号编码" align="center" prop="mid" width="50px" />
      <el-table-column label="挂号专家" align="center" prop="makdDoctorId" />
      <el-table-column label="挂号时间" align="center" prop="makeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.makeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所患疾病" align="center" prop="makeStatus" />
      <el-table-column label="你的姓名" align="center" prop="makeName" width="80px" />
      <el-table-column label="联系方式" align="center" prop="makePhone" />
      <el-table-column label="预约状态" align="center" prop="makeCancel" width="80px">
        <template slot-scope="scope">{{ scope.row.makeCancel==1?'已预约':'已取消' }}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:make:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-phone"
            @click="handleUpdate2(scope.row)"
            v-hasPermi="['system:make:edit']"
            :disabled="new Date(scope.row.makeDate).getTime()>new Date().getTime()?false:true"
            v-if="scope.row.makeCancel==1?true:false"
          >取消预约</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:make:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改挂号信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="挂号专家" prop="makdDoctorId">
          <el-input v-model="form.makdDoctorId" disabled />
        </el-form-item>
        <el-form-item label="挂号时间" prop="makeDate">
          <el-date-picker clearable size="small"
            v-model="form.makeDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择挂号时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="所患疾病" prop="makeStatus">
          <el-input v-model="form.makeStatus" placeholder="请输入所患疾病" />
        </el-form-item>
        <el-form-item label="你的姓名" prop="makeName">
          <el-input v-model="form.makeName" placeholder="请输入你的姓名" />
        </el-form-item>
        <el-form-item label="联系方式" prop="makePhone">
          <el-input v-model="form.makePhone" placeholder="请输入联系方式" />
        </el-form-item>
        <!-- <el-form-item label="所属用户" prop="makeUser">
          <el-input v-model="form.makeUser" placeholder="请输入所属用户" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 取消预约挂号信息对话框 -->
    <el-dialog :title="title" :visible.sync="open2" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="挂号专家" prop="makdDoctorId">
          <el-input v-model="form.makdDoctorId" disabled />
        </el-form-item>
        <el-form-item label="挂号时间" prop="makeDate">
          <el-date-picker clearable size="small"
            v-model="form.makeDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择挂号时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="所患疾病" prop="makeStatus">
          <el-input v-model="form.makeStatus" placeholder="请输入所患疾病" />
        </el-form-item> -->
        <el-form-item label="你的姓名" prop="makeName">
          <el-input v-model="form.makeName" placeholder="请输入你的姓名" />
        </el-form-item>
        <el-form-item label="取消原因" prop="makeMessage">
          <el-input v-model="form.makeMessage" placeholder="请输入取消原因" />
        </el-form-item>
        <!-- <el-form-item label="所属用户" prop="makeUser">
          <el-input v-model="form.makeUser" placeholder="请输入所属用户" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMake, getMake, delMake, addMake, updateMake, exportMake } from "@/api/system/make";
import { getUserProfile,getUser } from "@/api/system/user";

export default {
  name: "Make",
  components: {
  },
  data() {
    return {
      //保存登录用户信息
      user: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 挂号信息表格数据
      makeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        makdDoctorId: null,
        makeDate: null,
        makeStatus: null,
        makeName: null,
        makePhone: null,
        makeUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    //this.getUser();
    this.getList();
  },
  methods: {
    //查询当前登录用户信息
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data;
      });
    },
    /** 查询挂号信息列表 */
    getList() {
      this.loading = true;
      getUserProfile().then(response => {
        this.user = response.data;
        // console.log(this.user.userId);
        if(this.user.userId!="1"){
          this.queryParams.makeUser=this.user.userId;
        }
        listMake(this.queryParams).then(response => {
          this.makeList = response.rows;
          // console.log(this.makeList);
          this.total = response.total;
          this.loading = false;
        });
      });

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel2() {
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        mid: null,
        makdDoctorId: null,
        makeDate: null,
        makeStatus: null,
        makeName: null,
        makePhone: null,
        makeUser: null,
        makeMessage: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.mid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加挂号信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const mid = row.mid || this.ids
      getMake(mid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改挂号信息";
      });
    },
    /** 取消预约按钮操作 */
    handleUpdate2(row) {
      this.reset();
      const mid = row.mid || this.ids
      getMake(mid).then(response => {
        this.form = response.data;
        this.open2 = true;
        this.title = "取消预约挂号信息";
      });
    },
    /** 取消预约提交 */
    submitForm2() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.mid != null) {
            this.form.makeCancel = 0;
            updateMake(this.form).then(response => {
              this.msgSuccess("取消成功");
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.mid != null) {
            updateMake(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.makeCancel = 1;
            addMake(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const mids = row.mid || this.ids;
      this.$confirm('是否确认删除挂号信息编号为"' + mids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMake(mids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有挂号信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMake(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
